package cn.bnjy.controller;

import cn.bnjy.core.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;

/**
 * 描述：
 *
 * @date: 2018/11/3 13:39
 * @author: chavin
 */
@ControllerAdvice
@Slf4j
public class ControllerExceptionAdvice {

    @ExceptionHandler
    public Result handler(HttpServletRequest req, HttpServletResponse res, Exception e) {
        if (e instanceof NullPointerException) {
            log.error("空指针异常：", e);
            return Result.error("发生空指针异常");
        } else if (e instanceof IllegalArgumentException) {
            log.error("请求参数异常：", e);
            return Result.error("请求参数异常");
        } else if (e instanceof SQLException) {
            log.error("数据库异常：", e);
            return Result.error("数据库异常");
        } else {
            return Result.error("服务器异常,请联系管理员");
        }
    }

}
